//package com.chen.web005;
//
//import com.jspsmart.upload.File;
//import com.jspsmart.upload.SmartUpload;
//import com.jspsmart.upload.SmartUploadException;
//import jakarta.servlet.ServletException;
//import jakarta.servlet.annotation.WebServlet;
//import jakarta.servlet.http.HttpServlet;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//
//import javax.servlet.jsp.JspFactory;
//import javax.servlet.jsp.PageContext;
//import java.io.IOException;
//
//@WebServlet(urlPatterns = "/toupload")
//public class UploadServlet extends HttpServlet {
//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        doPost(req, resp);
//    }
//
//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        try {
//            //创建对象
//            SmartUpload smartUpload = new SmartUpload();
//            //初始化
//            JspFactory defaultFactory = JspFactory.getDefaultFactory();
//            PageContext pageContext = defaultFactory.getPageContext(this, req, resp, null, false, 1024, true);
//
//            smartUpload.initialize(pageContext);
//            smartUpload.setCharset("UTF-8");
//
//            //上传
//            smartUpload.upload();
//            //指定上传路径
//            File file = smartUpload.getFiles().getFile(0);
//            String fileName = file.getFileName();
//            String url = "/myuploadfile/" + fileName;
//            //文件保存
//            file.saveAs(url,File.SAVEAS_VIRTUAL);
//            //得到上传的普通问题本
//            String uname = smartUpload.getRequest().getParameter("uname");
//            //跳转页面
//            req.setAttribute("username", uname);
//            req.setAttribute("imgname", fileName);
//            req.getRequestDispatcher("success2.jsp").forward(req, resp);
//        } catch (SmartUploadException e) {
//            throw new RuntimeException(e);
//        }
//
//    }
//}
